/**
 * 
 */
package com.rangers.auth.base.service;

import java.io.Serializable;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.rangers.auth.base.db.RoleDbAccess;
import com.rangers.auth.base.details.BriefRole;

/**
 * 角色sevice
 * 
 * @version 1.0
 * @author fuhw
 * @date 2017年8月18日 下午12:22:57
 */
@SuppressWarnings("rawtypes")
@Service
public class RoleServiceHandler<T extends BriefRole, ID extends Serializable,ACCESS extends RoleDbAccess<T, ID>> implements RoleService<T, ID,ACCESS> {

	private final ACCESS access;

	@Autowired
	public RoleServiceHandler(ACCESS roleRepo) {
		super();
		this.access = roleRepo;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.rangers.auth.base.service.RoleService#get(java.io.Serializable)
	 */
	@Override
	public T get(ID id) {
		Assert.notNull(id, "the token of object must be not null");
		return access.findOne(id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.rangers.auth.base.service.RoleService#list()
	 */
	@Override
	public List<T> list() {
		return (List<T>) access.findAll();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.rangers.auth.base.service.RoleService#create(com.rangers.auth.base.
	 * entity.BriefRole)
	 */
	@Override
	@Transactional
	public T create(T entity) {
		argumentValidate(true, entity);
		return access.save(entity);
	}

	private void argumentValidate(boolean isAdd, T entity) {
		Assert.notNull(entity, "the entity for create must be not null");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.rangers.auth.base.service.RoleService#update(com.rangers.auth.base.
	 * entity.BriefRole)
	 */
	@Override
	@Transactional
	public T update(T entity) {
		argumentValidate(true, entity);
		return access.save(entity);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.rangers.auth.base.service.RoleService#delete(java.io.Serializable)
	 */
	@Override
	public void delete(ID id) {
		access.delete(id);
	}
}
